package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiantian.dao.UserMessagesRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Usermessages;
import com.tiantian.service.UserMessagesService;

@Service
@Transactional
public class UserMessagesServiceImpl extends GenericServiceImpl<Usermessages, String> implements UserMessagesService {
	private UserMessagesRepository repository;

	@Autowired
	public void setRepository(UserMessagesRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	public String getMsgNo(String sendTime) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		sql.append("select max(msgNO) msgNO from UserMessages a");
		sql.append("  where 1=1 ");
		if (StringUtils.isNotBlank(sendTime)) {
			sql.append(" and DATE_FORMAT(sendtime,'%Y-%m-%d')=:sendTime ");
			params.put("sendTime", sendTime);
		}
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(sql.toString(), params);
		if (ret != null && ret.size() > 0) {
			if (ret.get(0).get("msgno") != null) {
				return ret.get(0).get("msgno") + "";
			}
		}
		return null;
	}

}
